<?php
set_time_limit(0);

include_once (dirname(__FILE__).'/config/database.php');
include_once (dirname(__FILE__).'/class/Log.inc');
include_once (dirname(__FILE__).'/class/Mysql/Exception.inc');
include_once (dirname(__FILE__).'/class/Mysql.inc');

define('LOCK_FILE', dirname(__FILE__).'/lock/countPay.lock');
define('LOG_LEVEL', 'ERROR');

$LogFile = dirname(__FILE__).'/log/countPay'.date('Y.m.d').'.log';

$Log = new Zw_Log();
$Log->setCurrLogLevel(LOG_LEVEL);
$Log->setLogFile($LogFile);

$handle = fopen(LOCK_FILE, 'w+');
if(!flock($handle, LOCK_EX|LOCK_NB)){
	$Log->write('进程已经启动，请不要重复启动');
	exit();
}

$GmDb = new Zw_Mysql();
$GmDb->setLog($Log);
$GmDb->connect($gmDatabaseHost, $gmDatabaseUsername, $gmDatabasePassword, $gmDatabaseName);

$GameDb = new Zw_Mysql();
$GameDb->setLog($Log);
$count = 0;
$a=array();
	try {
		$sql = "
			select
				e.`ipAddress`,
				c.`databaseName`,
				c.`domain`

			from

				`t_data_game_area` c

			left join
				`t_data_server_ip` d
			on
				c.`serverId` = d.`serverId`
			left join
				`t_data_ip` e
			on
				d.`ipId` = e.`ipId`
			left join
				`t_cfg_ip_type` f
			on
				e.`ipTypeId` = f.`ipTypeId`
			where
				f.`isDefault` = 1
		";
		$IpArray = $GmDb->getArray($sql);

		foreach ($IpArray as $k1 => $v1) {
			$a[$v1['operatorName']] = 0;
		}

		foreach ($IpArray as $k1 => $v1) {
			$GameDb->connect($v1['ipAddress'], $gameDatabaseUsername, $gameDatabasePassword, $v1['databaseName']);

			$sql = "
					select SUM(Money) money from t_log_pay where utime > UNIX_TIMESTAMP('2017-04-26') and  utime < UNIX_TIMESTAMP('2017-04-27')
				";

			$result = $GameDb->getRow($sql);

			if($result['money']){
				$a[$v1['domain']] = $result['money'];
				//$Log->write($v1['domain']."-".$result['money'], 'ERROR');
				file_put_contents($this->logFile, $v1['domain']."-".$result['money'], FILE_APPEND);
				$count += $result['money'];
				}


		}
		$Log->write("总数-".$count, 'ERROR');
		$Log->write("总数-".json_encode($a), 'ERROR');

	}

	catch(Zw_Mysql_Exception $e) {
		$Log->write('数据库错误', 'ERROR');
	}
	catch(Exception $e) {
		$Log->write('系统错误', 'ERROR');
	}
